{extend name="layout/layout" /}

{block name="pagecss"}
{load href="__STATIC__/plugins/jstree/dist/themes/default/style.min.css" /}
{/block}

{block name="content"}
<div class="row">
    <div class="col-xs-12">
        <div class="box box-success">
            <div class="box-header with-border">
                <h3 class="box-title">添加</h3>
                <div class="box-tools">
                    <!--<button class="btn bg-navy btn-sm" data-toggle="tooltip" title="" data-original-title="Back" onclick="history.go(-1);">
                        <i class="fa fa-reply"></i> 返回
                    </button>-->
                </div>
            </div>
            <div class="box-body">
                <form action="{:url('admin/'.strtolower($model).'/addpost')}" class="form-horizontal form validation-form" method="post" enctype="multipart/form-data">
                    {:token()}
                    <div class="form-body">
                        <div class="row">
                            <div class="col-md-9">
                                <div class="form-group">
                                    <label class="control-label col-md-3">
                                        当前组织
                                    </label>
                                    <div class="col-md-9">
                                        <span class="form-control">{$organization.name}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-9">
                                <div class="form-group">
                                    <label class="control-label col-md-3">
                                        职务名称
                                        <span class="required">*</span>
                                    </label>
                                    <div class="col-md-9">
                                        <input type="text" class="form-control" id="name" data-required="1" name="name" value="" maxlength="65">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-9">
                                <div class="form-group">
                                    <label class="control-label col-md-3">
                                        排序
                                    </label>
                                    <div class="col-md-9">
                                        <input type="text" class="form-control" id="sorting" data-required="1" name="sorting" value="">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-9">
                                <div class="form-group">
                                    <label class="control-label col-md-3">
                                        管理权限
                                    </label>
                                    <div class="col-md-9">
                                        <div id="access-tree">
                                            {$accessTree}
                                            <input type="hidden" name="rules" id="rules" value="" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <input type="hidden" name="organizeid" value="{$organization.id}">

                        <hr />

                        <div class="form-actions">
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label class="col-md-3 control-label"></label>
                                        <div class="col-md-9">
                                            <button type="submit" class="btn btn-primary">
                                                保存 <i class="fa fa-floppy-o"></i>
                                            </button>
                                            <button type="button" class="btn btn-default" onclick="history.go(-1);">
                                                返回 <i class="fa fa-mail-forward"></i>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="pagescript"}
{load href="__STATIC__/plugins/jstree/dist/jstree.min.js" /}
<script type="text/javascript">
    window.onload = function () {
        var treeObj = $("#access-tree>div").first();

        treeObj.on('changed.jstree', function (e, data) {
            var i, j, k, r = [];
            for (i = 0, j = data.selected.length; i < j; i++) {
                r.push($('#' + data.selected[i]).attr('rel'));
                //set parents node
                var parents = data.instance.get_node(data.selected[i]).parents;
                if (parents) {
                    for (k = 0; k < parents.length; k++) {
                        if (parents[k].indexOf('tree-node') > -1) {
                            r.push($('#' + parents[k]).attr('rel'));
                        }
                    }
                }
            }
            r = $.unique(r);
            $('#rules').val(r ? r.join(',') : '');
        }).jstree({
            "plugins": ["checkbox"],
            "checkbox": {
                "three_state": true,
                "cascade": 'up+undetermined'
            },
            "core": {
                "themes": {
                    responsive: !1
                }
            }
        });
        treeObj.jstree('open_all');

        var checked = $('#rules').val().split(',');
        if (checked) {
            for (var i in checked) {
                treeObj.jstree('select_node', treeObj.attr('id') + '-' + checked[i]);
            }
        }

    };

    $(".validation-form").validate({
        submitHandler:function(form){
            if(!checkForm(form)){
                return false;
            }

            form.submit();
        }
    });

    /**
     * 表单验证
     */
    function checkForm(form){
        var name = $('#name').val();

        if(name.trim() == ''){
            layer.msg('<span style="font-size:20px">职务名称不能为空！</span>');
            return false;
        }

        return true;
    }
</script>
{/block}